
// @Author: Wijnen David [c1026300] 2013-11-19
// -------------------------------------------
// @Description: Deze view is voor luchthavens toe te voegen.

package views.createViews;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;

import controllers.createControllers.ToevoegenLuchthavenController;
import controllers.updateControllers.UpdateLuchthavenController;
import core.logic.services.utilServices.ValidatieService;
import core.models.dataModels.createModels.ToevoegenLuchthavenModel;
import core.models.domainModel.Adres;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ToevoegenLuchthavenView extends JFrame {
	
	//standaard versie ID
	private static final long serialVersionUID = 1L;
	
	private JTextField txtCode;
	private JTextField txtOmschrijving;
	private JTextField txtStraat;
	private JTextField txtHuisnummer;
	private JTextField txtBus;
	private JTextField txtGemeente;
	private JTextField txtPostcode;
	private JTextField txtStaat;
	private JTextField txtLand;
	private ToevoegenLuchthavenModel luchthavenModel;
	private JButton btnOk;
	private JButton btnCancel;
	
	public ToevoegenLuchthavenView(final ToevoegenLuchthavenModel model) {
		this.luchthavenModel = model;
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("Toevoegen Luchthaven");
		
		// knoppen aanmaken
		btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == btnOk)
				{
						switch(luchthavenModel.getSchermModus()) {
							case "CREATE":
								voegLuchthavenToe();
								break;
							case "UPDATE":
								updateLuchthaven();
								break;
							case "DELETE":
								deleteLuchthaven();
								break;
							case "VIEW":
								break;
							default:
								break;
						}
				}
			}
		});
			
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(arg0.getSource() == btnCancel)
				{
					// het scherm sluiten
					switch( luchthavenModel.getSchermModus()) {
						case "CREATE":
							ToevoegenLuchthavenController.closeView(luchthavenModel);
							break;
						case "UPDATE":
							UpdateLuchthavenController.closeView(luchthavenModel);
							break;
						case "DELETE":
							deleteLuchthaven();
							break;
						case "VIEW":
							break;
						default:
							break;
					}
				}
			}
		});
			
		btnCancel.setToolTipText("Toevoegen annuleren");
		
		// Labels aanmaken
		JLabel lblCode = new JLabel("Code:");
		JLabel lblOmschrijving = new JLabel("Omschrijving:");
		JLabel lblStraat = new JLabel("Straat:");
		JLabel lblHuisnummer = new JLabel("Huisnummer:");
		JLabel lblBus = new JLabel("Bus:");
		JLabel lblGemeente = new JLabel("Gemeente:");
		JLabel lblPostcode = new JLabel("Postcode:");
		JLabel lblStaatprovincie = new JLabel("Staat/Provincie:");
		JLabel lblLand = new JLabel("Land:");
		
		// tekstvelden instellen
		txtCode = new JTextField();
		txtCode.setColumns(10);
		
		txtOmschrijving = new JTextField();
		txtOmschrijving.setColumns(10);
		
		txtStraat = new JTextField();
		txtStraat.setColumns(10);
		
		txtHuisnummer = new JTextField();
		txtHuisnummer.setColumns(10);
		
		txtBus = new JTextField();
		txtBus.setColumns(10);
		
		txtGemeente = new JTextField();
		txtGemeente.setColumns(10);
		
		txtPostcode = new JTextField();
		txtPostcode.setColumns(10);
		
		txtStaat = new JTextField();
		txtStaat.setColumns(10);
		
		txtLand = new JTextField();
		txtLand.setColumns(10);
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(17)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblLand, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addGap(10)
								.addComponent(txtLand, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(btnCancel, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
								.addGap(107)
								.addComponent(btnOk, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(lblStaatprovincie, GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
								.addComponent(lblHuisnummer, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(lblBus, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(lblGemeente, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(lblPostcode, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(lblStraat, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(lblOmschrijving, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(lblCode, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addGap(10)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(txtGemeente, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtStraat, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtOmschrijving, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtStaat, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)
								.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
									.addComponent(txtBus, Alignment.LEADING)
									.addComponent(txtHuisnummer, Alignment.LEADING))
								.addComponent(txtPostcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap(86, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblCode))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtOmschrijving, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblOmschrijving))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtStraat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblStraat))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtHuisnummer, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblHuisnummer))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtBus, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblBus))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtGemeente, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblGemeente))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtPostcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblPostcode))
					.addGap(8)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtStaat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblStaatprovincie))
					.addGap(7)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtLand, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblLand))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnOk, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnCancel))
					.addGap(44))
		);
		getContentPane().setLayout(groupLayout);
		
		switch (luchthavenModel.getSchermModus()) {
			case "CREATE":
				this.setTitle("Toevoegen luchthavens");
				btnOk.setText("Toevoegen");
				break;
			case "UPDATE":
				this.setTitle("Update luchthaven " + luchthavenModel.getLuchthavenId());
				btnOk.setText("Updaten");				
				vulLuchthavenGegevens();
				break;
			case "DELETE":
				break;
			case "VIEW":
				break;
			default:
				break;
		}
	}
	
	private void vulLuchthavenGegevens() {
		txtCode.setText(luchthavenModel.getNieuweLuchthaven().getLuchthavenCode());
		txtOmschrijving.setText(luchthavenModel.getNieuweLuchthaven().getLuchthavenNaam());
		txtStraat.setText(luchthavenModel.getNieuweLuchthaven().getAdres().getStraat());
		txtHuisnummer.setText(Integer.toString(luchthavenModel.getNieuweLuchthaven().getAdres().getHuisnummer()));
		txtBus.setText(luchthavenModel.getNieuweLuchthaven().getAdres().getBus());
		txtPostcode.setText(luchthavenModel.getNieuweLuchthaven().getAdres().getPostcode());
		txtGemeente.setText(luchthavenModel.getNieuweLuchthaven().getAdres().getGemeente());
		txtStaat.setText(luchthavenModel.getNieuweLuchthaven().getAdres().getStaatProvincie());
		txtLand.setText(luchthavenModel.getNieuweLuchthaven().getAdres().getLand());
	}
	
	private static Boolean validateLuchthavenIngave(ToevoegenLuchthavenView scherm)
	{
		// Veld Code Controleren
		if(!ValidatieService.isPresentAndLength(scherm.txtCode, 3, "Code"))
			return false;
		
		if(!ValidatieService.isPresentAndLength(scherm.txtOmschrijving, 150, "Omschrijving"))
			return false;
		
		if(!ValidatieService.isPresentAndLength(scherm.txtStraat, 150, "Straat"))
			return false;
		
		if(!ValidatieService.isInteger(scherm.txtHuisnummer, "Huisnummer"))
			return false;
	
		if(!ValidatieService.isLength(scherm.txtBus, 10, "Bus"))
			return false;
		
		if(!ValidatieService.isPresentAndLength(scherm.txtGemeente, 150, "Gemeente"))
			return false;
		
		if(!ValidatieService.isPresentAndLength(scherm.txtPostcode, 20, "Postcode"))
			return false;
		
		if(!ValidatieService.isLength(scherm.txtStaat, 150, "Staat/Provincie"))
			return false;
		
		if(!ValidatieService.isPresentAndLength(scherm.txtLand, 150, "Land"))
			return false;
		
		return true;
	}
	
	private void voegLuchthavenToe() {

		// controleren of alle velden goed ingevuld zijn
		if(validateLuchthavenIngave(ToevoegenLuchthavenView.this))
		{
			// het model invullen met huidige waarden
			luchthavenModel.setCode(txtCode.getText());
			luchthavenModel.setNaam(txtOmschrijving.getText());
			luchthavenModel.setAdres(new Adres(txtStaat.getText(),Integer.parseInt(txtHuisnummer.getText()),txtBus.getText(), txtGemeente.getText(), txtPostcode.getText(), txtStaat.getText(), txtLand.getText()));
		
			// ingevuld model doorsturen naar de contoller
			ToevoegenLuchthavenController.handleView(luchthavenModel);
		}
	}
	
	private void updateLuchthaven() {
		// controleren of alle velden goed ingevuld zijn
		if(validateLuchthavenIngave(ToevoegenLuchthavenView.this)) {
			// Vul het model in met de gegevens in het scherm.
			luchthavenModel.setCode(txtCode.getText());
			luchthavenModel.setNaam(txtOmschrijving.getText());
			Adres adres = new Adres(
					txtStaat.getText(),
					Integer.parseInt(txtHuisnummer.getText()),
					txtBus.getText(), 
					txtGemeente.getText(), 
					txtPostcode.getText(), 
					txtStaat.getText(), 
					txtLand.getText());
			adres.set_adresId(luchthavenModel.getNieuweLuchthaven().getAdres().get_adresId());
			luchthavenModel.setAdres(adres);
			JOptionPane.showMessageDialog(null, luchthavenModel.getNaam() + " is aangepast" );
			// Stuur het model door naar de controller
			UpdateLuchthavenController.handleView(luchthavenModel);
		}
	}
	
	private void deleteLuchthaven() {
		
	}
}
